package com.example.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.example.domain.User;
import com.example.domain.UserRepository;
import com.example.handler.BusinessErrorException;

@Service
public class UserService extends BaseService<User, Long> {
    public UserService(JpaRepository<User, Long> dao) {
        super(dao);

    }

    @Autowired
    private UserRepository userDao;

    public User findByName(String name) {
        return userDao.findByName(name);

    }

    public User findByNameAndAge(String name, Integer age) {
        return userDao.findByNameAndAge(name, age);

    }

    /**
     * 
     * @description 默认是runtimeexception及其子类才会rollback
     * @author lli
     * @create 2017年4月13日下午4:00:38
     * @version 1.0
     * @throws Exception
     */
    @Transactional(rollbackFor = Exception.class)
    public void test() throws Exception {
        for (int i = 0; i < 3; i++) {
            User entity = new User();
            entity.setAge(i);
            userDao.save(entity);
            if (i == 2) {
                throw new BusinessErrorException();
            }
        }

    }

}
